//
//  CityTableView.m
//  Project
//
//  Created by 冯东辉 on 2018/9/19.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "CityTableView.h"

@implementation CityTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style data:(NSArray *)data {
    
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        _city_data = data;
        
        self.backgroundColor = Base_color;
        self.delegate = self;
        self.dataSource = (id)self;
        self.bounces = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *city_dic = _city_data[indexPath.row];
    
    if ([_city_delegate respondsToSelector:@selector(cityChooseCallBack:)]) {
        
        [_city_delegate cityChooseCallBack:city_dic];
    }
}

#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_city_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier_city = @"city_cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier_city];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier_city];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = Base_color;
        cell.contentView.backgroundColor = Base_color;
        
        UILabel *cityL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
        cityL.tag = 100;
        cityL.textColor = UICOLOR_HEX(0xffffffff);
        cityL.textAlignment = NSTextAlignmentCenter;
        cityL.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:cityL];
    }
    
    UILabel *city_l = [cell.contentView viewWithTag:100];
    
    city_l.text = [_city_data objectAtIndex:indexPath.row][@"cityname"];
    
    
    return cell;
}

@end
